﻿using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
using System.Xml;
using SHomeWorkshop.LunarPresentation.Interfaces;
using SHomeWorkshop.LunarPresentation.Widegets;
using WPF = System.Windows.Controls;
using System;

namespace SHomeWorkshop.LunarPresentation.Expressions
{
    public class Row : Expression, IDirection, ICollapsed
    {
        public Row(SubPanel rootPanel, SubPanel parentPanel, Editor editor) :
            base(rootPanel, parentPanel, editor)
        {
            this.mainSubPanel = new SubPanel(rootPanel, null, editor, this);
            this.mainBorder.Child = this.mainSubPanel;
            this.subPanels = new List<SubPanel>();

            this.subPanels.Add(this.mainSubPanel);
            this.firstSubPanel = this.lastSubPanel = this.mainSubPanel;

            this.chineseName = "行";
            this.borderColor = null;
        }

        private static Thickness cMargin = new Thickness(1);

        private static Thickness noMargin = new Thickness(0);

        private SubPanel mainSubPanel;

        public SubPanel MainSubPanel
        {
            get { return mainSubPanel; }
        }

        protected override void BuildChildren()
        {
            base.BuildChildren();

            if (XmlData == null) return;

            //全部清空。再重建。
            this.mainSubPanel.BasePanel.Children.Clear();

            XmlNode panelNode = this.XmlData.SelectSingleNode("Expression");
            if (panelNode.GetAttributeString("Type") == TypeOfExpression.SubPanel.ToString())
            {
                this.mainSubPanel.XmlData = panelNode;
            }

            XmlAttribute attrIsCollapsed = this.XmlData.GetAttribute("IsCollapsed");
            if (attrIsCollapsed != null)
            {
                this.isCollapsed = bool.Parse(attrIsCollapsed.Value);
                this.RefreshIsCollapsed();
            }

            //2011年7月16日，“行”也添加自动边框色（与编辑器默认前景色同）。
            RefreshDefBorderColor();
        }

        private void RefreshDefBorderColor()
        {
            XmlAttribute attrBorderColor = this.XmlData.GetAttribute("BorderColor");
            if (attrBorderColor == null)
            {
                //Binding borderBinding = new Binding();
                //borderBinding.Source = this.Editor.DefaultForeground;
                //this.mainBorder.SetBinding(Border.BorderBrushProperty, borderBinding);

                //考虑到“行”不需要自动以前景色的方式出现。
                //且未找到“取消绑定”的办法。此处未使用“绑定”来实现。
                //更改编辑器“默认前景色”后，将使用递归来实现“自动边框前景色”的更新。
                if (this.editor != null)
                {
                    this.mainBorder.BorderBrush = this.Editor.DefaultForeground;
                }
            }
            else
            {
                Brush resultBsh = ColorPicker.GetBrushByName(attrBorderColor.Value);
                if (resultBsh == null)
                {
                    this.borderColor = null;
                    this.mainBorder.BorderBrush = editor.DefaultForeground;
                }
                else
                {
                    this.mainBorder.BorderBrush = resultBsh;
                }
            }
        }

        public WPF.Orientation Direction
        {
            get
            {
                if (mainSubPanel != null)
                {
                    return mainSubPanel.Direction;
                }
                else return WPF.Orientation.Horizontal;//默认是水平向
            }
            set
            {
                if (mainSubPanel != null)
                {
                    mainSubPanel.Direction = value;
                }
            }
        }



        public override string CommentText
        {
            get
            {
                return base.CommentText;
            }
            set
            {
                base.CommentText = value;
                if (isCollapsed && exCollapseMark != null)
                {
                    exCollapseMark.RefreshExCollpseMarkText();
                }
            }
        }

        #region ICollpased接口的实现代码。

        private ExCollapseMark exCollapseMark;

        private bool isCollapsed = false;

        public bool IsCollapsed
        {
            get { return isCollapsed; }
            set
            {
                isCollapsed = value;
                RefreshIsCollapsed();

                this.XmlData.SetAttribute("IsCollapsed", isCollapsed.ToString());

                Editor.IsModified = true;
            }
        }

        private void RefreshIsCollapsed()
        {
            if (isCollapsed)
            {
                if (exCollapseMark == null)
                {
                    exCollapseMark = new ExCollapseMark(this);
                }

                exCollapseMark.RefreshExCollpseMarkText();
                this.mainBorder.Child = exCollapseMark;
            }
            else
            {
                this.mainBorder.Child = mainSubPanel;
                this.exCollapseMark = null;
            }

            this.InvalidateVisual();
        }

        #endregion

        public override void SetProperty(string propertyName, string value)
        {
            if (propertyName == null || propertyName.Length <= 0) return;
            base.SetProperty(propertyName, value);

            switch (propertyName)
            {
                case "Direction":
                    {
                        this.Direction = (WPF.Orientation)Enum.Parse(typeof(WPF.Orientation), value);
                        break;
                    }
                case "IsCollapsed":
                    {
                        this.IsCollapsed = bool.Parse(value);
                        break;
                    }
                case "BorderColor":
                    {
                        if (value == "UnKnown")
                        {
                            this.XmlData.RemoveAttributeByName("BorderColor");
                        }

                        RefreshDefBorderColor();
                        break;
                    }
            }
        }

        public override void UpdateExForegroundWithoutXmlChanging(Brush value)
        {
            base.UpdateExForegroundWithoutXmlChanging(value);

            base.exForeground = value;

            if (this.exCollapseMark != null)
            {
                this.exCollapseMark.UpdateForeground();
            }
            else
            {
                RefreshDefBorderColor();
            }
        }
    }
}
